package cn.iocoder.yudao.module.book.controller.admin.plans;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.book.controller.admin.plans.vo.*;
import cn.iocoder.yudao.module.book.dal.dataobject.plans.PlansDO;
import cn.iocoder.yudao.module.book.service.plans.PlansService;

@Tag(name = "管理后台 - 阅读计划")
@RestController
@RequestMapping("/book/plans")
@Validated
public class PlansController {

    @Resource
    private PlansService plansService;

    @PostMapping("/create")
    @Operation(summary = "创建阅读计划")
    @PreAuthorize("@ss.hasPermission('book:plans:create')")
    public CommonResult<Integer> createPlans(@Valid @RequestBody PlansSaveReqVO createReqVO) {
        return success(plansService.createPlans(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新阅读计划")
    @PreAuthorize("@ss.hasPermission('book:plans:update')")
    public CommonResult<Boolean> updatePlans(@Valid @RequestBody PlansSaveReqVO updateReqVO) {
        plansService.updatePlans(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除阅读计划")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('book:plans:delete')")
    public CommonResult<Boolean> deletePlans(@RequestParam("id") Integer id) {
        plansService.deletePlans(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得阅读计划")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('book:plans:query')")
    public CommonResult<PlansRespVO> getPlans(@RequestParam("id") Integer id) {
        PlansDO plans = plansService.getPlans(id);
        return success(BeanUtils.toBean(plans, PlansRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得阅读计划分页")
    @PreAuthorize("@ss.hasPermission('book:plans:query')")
    public CommonResult<PageResult<PlansRespVO>> getPlansPage(@Valid PlansPageReqVO pageReqVO) {
        PageResult<PlansDO> pageResult = plansService.getPlansPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PlansRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出阅读计划 Excel")
    @PreAuthorize("@ss.hasPermission('book:plans:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPlansExcel(@Valid PlansPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PlansDO> list = plansService.getPlansPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "阅读计划.xls", "数据", PlansRespVO.class,
                        BeanUtils.toBean(list, PlansRespVO.class));
    }

}